﻿<?php
@session_start();
include_once("../initial.php");
include_once(constant('PROJECT_DIR')."/logic/classLogic.php");
//echo constant('PROJECT_DIR')."/logic/classLogic.php";
$DOCUMENT_ROOT=$_SERVER["DOCUMENT_ROOT"];
$smarty->assign("projectUrl", constant('PROJECT_URL'));
$LIST_NUMBER_PER_PAGE=10;
require_once(constant('PROJECT_DIR')."/checkLogin.php");
require_once(constant('PROJECT_DIR')."/checkAdminLogin.php");
$adminId=$_SESSION['user'];

$action="";
if(isset($_POST['action']))
{
     $action = $_POST['action'];
}
else if(isset($_GET['action']))
{
     $action=$_GET['action'];
}
$cl = new classLogic();
$searchResult=array();
$searchNote="";
switch($action)
{
     case "addclass":
         $classname=$_POST['name'];
         $classinfo=$_POST['info'];
         $monitor=$_POST['monitor'];
         $teacher=$_POST['teacher'];
         if($classname == "")
         {
             break;
         }
         $array = array();
         $array['name']=$classname;
         $array['info']=$classinfo;
         $array['monitorid']=$monitor;
         $array['teacherid']=$teacher;

         $cl->insert_class($array);
         break;
    case "deleteclass":
        $classids=$_POST['classid'];
        foreach($classids as $classid)
        {
            echo $classid;
            $cl->delete_class($classid);
        }
        break;
    case "modifyclass":
        $classid=$_POST['classid'];
        if($classid=="")
        {
            break;
        }
        $classname=$_POST['name'];
        $info=$_POST['info'];
        $monitor=$_POST['monitor'];
        $teacher=$_POST['teacher'];
        $modifyarray=array();
        $modifyarray["name"]=$name;
        $modifyarray["info"]=$info;
        $modifyarray["monitorid"]=$monitor;
        $modifyarray["teacherid"]=$teacher;
        $cl->update_class($modifyarray, $classid);
        break;
    case "search":
				$keyword = trim($_GET['keyword']);
    		if(strlen($keyword)<3)
    		{
    				$searchNote .="输入的关键词太短，不进行搜索,请输入长度超过3的字符串.";
    				$smarty->assign("searchNote", $searchNote);
    		}
    		//keyword长度要超过2
    		$searchResult = $cl->searchByKeyword($keyword, "limit 0,30");//默认显示
    		if(count($searchResult)==30)
    		{
    				$searchNote .="搜索词得到太多结果，只显示30条，请输入更长的搜索词";
    				$smarty->assign("searchNote", $searchNote);
    		}
    		if(count($searchResult)==0)
    		{
    				$searchNote .="没有结果，展示所有结果";
    				$smarty->assign("searchNote", $searchNote);
    		}
    		$smarty->assign("keyword", $keyword);
    		break;
}

//handling the page index
$classNumber=0;
if(count($searchResult)>0)
{
		$classNumber=count($searchResult);
}
else
{
		$classNumber=$cl->get_class_num();
}

$pageIndex=1;
$totalPageNumber=ceil($classNumber/$LIST_NUMBER_PER_PAGE);
if(isset($_GET['pageIndex']))
{
     $pageIndex=$_GET['pageIndex'];
     if($pageIndex<1 || $pageIndex > $totalPageNumber)
     {
         $pageIndex=1;
     }
}
$startItem=($pageIndex-1)*$LIST_NUMBER_PER_PAGE;
$ItemNumber=$LIST_NUMBER_PER_PAGE;

$leftPageIndex=$pageIndex<=1?1:$pageIndex-1;
$rightPageIndex=$pageIndex>=$totalPageNumber?$totalPageNumber:$pageIndex+1;
$leftMostPageIndex=$pageIndex<=5?1:$pageIndex-5;//左边显示5个页面索引
$rightMostPageIndex=$totalPageNumber<$pageIndex+5?$totalPageNumber:$pageIndex+5;//右边显示5个页面索引
$smarty->assign("leftMostPageIndex",$leftMostPageIndex);
$smarty->assign("rightMostPageIndex",$rightMostPageIndex);
$smarty->assign("leftPageIndex",$leftPageIndex);
$smarty->assign("rightPageIndex",$rightPageIndex);
$smarty->assign("pageIndex",$pageIndex);
$smarty->assign("classNumber", $classNumber);

//get the edit class
if(isset($_GET['action']))
{
     if($_GET['action'] == "edit")
     {
         $classid=$_GET['id'];
         $classinfo = $cl->get_class(array("id"=>$classid));
         $smarty->assign("oneclassinfo", $classinfo);
     }
}

$allclasses=array();
if(count($searchResult)>0)
{
		$allclasses=$searchResult;
}
else
{
		$allclasses = $cl->get_class('',"order by id desc", "limit $startItem,$ItemNumber");
}

$smarty->assign("classes", $allclasses);
$smarty->assign("menuSelected", "class");
$smarty->assign("currentUrl","admin/class.php");
$smarty->display("admin/class.html");
?>
